/**
 * Copyright 2019 [shujiang.yang], Inc. All rights reserved.
 * Email: 314938230@qq.com
**/
package awd.bj.jls.webs.controller;

import java.util.Date;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cloud.context.config.annotation.RefreshScope;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.google.common.collect.Maps;

import awd.bj.jls.model.JbxxModel;
import awd.bj.jls.model.XyrModel;
import awd.bj.jls.webs.api.JlsServersApi;
import awd.bj.jls.webs.api.model.User;
import awd.bj.jls.webs.utils.PagerResult;
import awd.bj.jls.webs.utils.QueryParam;
import awd.bj.jls.webs.utils.ResponseMessage;
import awd.bj.jls.webs.utils.Result;
import awd.bj.jls.webs.utils.ResultUtils;

@Controller
@RequestMapping("/jbxx")
@RefreshScope
public class JbxxAction extends BaseAction {

    @Autowired
    private JlsServersApi jlsServersApi;

    @RequestMapping({"/jbxx.html" //首页
            ,"/jbxx_enter.html" //单表录入页面
            ,"/jbxx_jbxx_enter.html" //人员信息查询页面
            ,"/jbxx_query.html" //信息查询
            ,"/jbxx_jbxx_query.html" //关联基本信息查询
            ,"/jbxx_yzpz.html" //验证凭证
            ,"/jbxx_ksrs.html" //快速入所
            ,"/jbxx_jydj.html" //简要登记
            ,"/jbxxFwdj_jbxx_enter.html" //附物登记
            ,"/jbxx_rsdj.html" //入所登记
            ,"/jbxxJkjc_jbxx_enter.html" //健康检查
            ,"/jbxx_lxcl.html" //另行处理
            ,"/jbxx_ldsp.html" //领导审批
            ,"/jbxxGzhdrjs_jbxx_enter.html" //告知后带入拘室
            ,"/7.html"})
    public String index(Model model, HttpServletRequest request) {
        model.addAllAttributes(getBaseModelData());
        return getRenderFilePath(request);
    }

/**
 @GetMapping("/jbxx")
 public ResponseMessage<PagerResult<JbxxModel>> jbxxQueryForPage(@RequestBody QueryParam param);
 @PostMapping("/jbxx")
 public ResponseMessage<String> jbxxSave(@RequestBody JbxxModel jbxxModel);
 @PutMapping("/jbxx/{id}")
 public String jbxxUpdate(@RequestParam(value="id") String id ,@RequestBody JbxxModel jbxxModel);
 */

    /**
     * 数据列表,关联基本信息
     * @param request
     * @param params 所有请求的参数集合
     * @return
     */
    @RequestMapping("/list2jbxx")
    @ResponseBody
    public Map<String, Object> list2jbxx(HttpServletRequest request,Map<String,Object> params) {
        QueryParam param = getBaseQueryParam(request,params);
        System.err.println("=======================何磊=========================");
        System.err.println("=======================查询数据====================");
        System.err.println(JSON.toJSONString(param));
        System.err.println("=======================何磊========================");

        ResponseMessage<PagerResult<JbxxModel>> list = jlsServersApi.jbxxQueryForPage(param);
        Map<String, Object> result = Maps.newHashMap();
        result.put("total", list.getResult().getTotal());
        result.put("rows", list.getResult().getData());
        return result;
    }

    /**
     * 数据列表
     * @param request
     * @param params 所有请求的参数集合
     * @return
     */
    @RequestMapping("/list")
    @ResponseBody
    public Map<String, Object> list(HttpServletRequest request,Map<String,Object> params) {
        QueryParam param = getBaseQueryParam(request,params);
        System.err.println("=======================何磊=======================");
        System.err.println("======================查询数据===================");
        System.err.println(JSON.toJSONString(param));
        System.err.println("=======================何磊=======================");

        ResponseMessage<PagerResult<JbxxModel>> list = jlsServersApi.jbxxQueryForPage(param);
        Map<String, Object> result = Maps.newHashMap();
        result.put("total", list.getResult().getTotal());
        result.put("rows", list.getResult().getData());
        return result;
    }

    /**
     * 数据保存操作
     //* @param model
     * @return
     */


    @RequestMapping("/save")
    @ResponseBody
    public Result<?> save(String jbxxDate,String ajxxDate,String filelsitJsonStr){
        User users = getCurrentUser();

        JbxxModel model= JSONObject.parseObject(jbxxDate, JbxxModel.class);
        XyrModel model1=JSONObject.parseObject(ajxxDate,XyrModel.class);

        try {
            model.setState("R8");
            model.setJsbh(users.getJsbh());
            model.setCreatetime(new Date());
            model.setCreator(users.getUsername());
            System.err.println("======================保存数据======================");
            System.err.println("基本信息："+JSON.toJSONString(model));
            System.err.println("上传图片名："+filelsitJsonStr);
            System.err.println("案件信息："+JSON.toJSONString(model1));
            System.err.println("===========================何磊=====================");
            return ResultUtils.success("保存成功");
            //return ResultUtils.success(jlsServersApi.jbxxSave(model));
        } catch (Exception e) {
            e.printStackTrace();
            return ResultUtils.error(Result.ERR_SAVE, "保存失败");
        }
    }

    @RequestMapping("/jbxxSave")
    @ResponseBody
    public Result<?> save(@ModelAttribute JbxxModel model){
        User users = getCurrentUser();
        try {
            model.setState("R8");
            model.setJsbh(users.getJsbh());
            model.setCreatetime(new Date());
            model.setCreator(users.getUsername());
            System.err.println("何磊：保存数据==========================================");
            System.err.println(JSON.toJSONString(model));
            System.err.println("==========================================================");
            return ResultUtils.success(jlsServersApi.jbxxSave(model));
        } catch (Exception e) {
            e.printStackTrace();
            return ResultUtils.error(Result.ERR_SAVE, "保存失败");
        }
    }
    



    /**
     * 数据更新
     * @param model
     * @return
     */
    @RequestMapping("/update")
    @ResponseBody
    public Result<?> update(@ModelAttribute JbxxModel model) {
        User users = getCurrentUser();
        try {
            model.setUpdator(users.getUsername());
            model.setUpdatetime(new Date());
            System.err.println("=======================何磊=========================");
            System.err.println("======================更新数据======================");
            System.err.println(JSON.toJSONString(model));
            System.err.println("===========================何磊=====================");
            return ResultUtils.success(jlsServersApi.jbxxUpdate(model.getId(),model));
        } catch (Exception e) {
            e.printStackTrace();
            return ResultUtils.error(Result.ERR_SAVE, "保存失败");
        }
    }

    /**
     * 删除操作
     * @param model
     * @return
     */
    @RequestMapping("/delete")
    @ResponseBody
    public Result<?> delete(@ModelAttribute JbxxModel model) {
        User users = getCurrentUser();
        try {
            model.setState("R3");
            model.setUpdator(users.getUsername());
            model.setUpdatetime(new Date());
            return ResultUtils.success(jlsServersApi.jbxxUpdate(model.getId(),model));
        } catch (Exception e) {
            e.printStackTrace();
            return ResultUtils.error(Result.ERR_SAVE, "删除失败");
        }
    }



}